package zilvic.game.room.entity;

import java.awt.Graphics;
import org.duncan.Math2D.Point2D;
import zilvic.resources.ByteMagic;
import zilvic.resources.Parsable;

/**
 * 
 * @author Duncan
 */
public abstract class Entity implements Parsable {
    private Point2D position;
    private long lastDelta = 0;

    public Entity(Point2D position) {
        this.position = position;
    }

    public void step(long delta) {
        lastDelta = delta;
    }

    public Point2D getPosition() {
        return position;
    }

    public Entity setPosition(Point2D position) {
        this.position = position;
        return this;
    }

    public abstract void paint(Graphics g, byte step);

    /**
     * The format is as follows.
     * 0:  The position (Point2D).
     * 1:  The last delta (long).
     * @return This Entity in bytes.
     */
    public byte[] toBytes() {
        return ByteMagic.compressBytes(new byte[][] {
            getPosition().toBytes(),
            ByteMagic.longToBytes(lastDelta)});
    }

    public void fromBytes(byte[] bytes) {
        byte[][] u = ByteMagic.uncompressBytes(bytes);
        getPosition().fromBytes(u[0]);
        lastDelta = ByteMagic.bytesToLong(u[1]);
    }
}